#include"mq_connection.hpp"

void cb(const xiang11mq::Channel::ptr &channel, const std::string &consumer_tag, const xiang11mq::BasicProperties *bp, const std::string &body)
{
    DLOG("客户端channeID: %s consumer_tag: %s 消费了消息 %s:", channel->cid().c_str(), consumer_tag.c_str(), body.c_str());
    channel->basicAck(bp->id());
}

int main(int argc, char *argv[])
{
    if(argc != 2)
    {
        std::cout << "usge: ./consumer_client queue1" << std::endl;
        return -1;
    }
    //1.实例化异步工作线程对象
    xiang11mq::AsyncWorker::ptr awp = std::make_shared<xiang11mq::AsyncWorker>();
    DLOG("实例化异步工作线程对象完成----------------------------------------");
    //2.实例化连接对象
    xiang11mq::Client::ptr conn = std::make_shared<xiang11mq::Client>("127.0.0.1", 8888, awp);
    if(conn.get() == nullptr)
    {
        DLOG("实例化连接对象为空");
        return 0;
    }
    DLOG("实例化连接对象完成--------------------------------------------------");
    //3.通过连接创建信道对象
    xiang11mq::Channel::ptr channel = conn->openChannel();
    // while(1)
    // {
    //     DLOG("");
    //     sleep(1);
    // }
    if(channel.get() == nullptr)
    {
        DLOG("通过连接创建信道对象完成 但是空对象--------------------------------------------");
    }
    else
        DLOG("通过连接创建信道对象完成--------------------------------------------");
    //4.通过信道提供的服务完成所需
    // 1.声明一个交换机exchange1，交换机类型为广播模式
    google::protobuf::Map<std::string, std::string> tmp_map;
    channel->declareExchange("exchange1", xiang11mq::ExchangeType::TOPIC, true, false, tmp_map);
    // 2.声明一个队列queue1
    channel->declareQueue("queue1", true, false, false, tmp_map);
    // 3.声明一个队列queue2
    channel->declareQueue("queue2", true, false, false, tmp_map);
    // 4.绑定queue1-exchange1，且binding key设置为queue1
    channel->queueBind("exchange1", "queue1", "queue1");
    // 5.绑定queue2-exchange1，且binding key设置为news.music.#
    channel->queueBind("exchange1", "queue2", "news.music.#");

    auto callbackfunc = std::bind(cb, channel, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3);
    channel->basicConsume("consumer1", argv[1], false, callbackfunc);

    while(1)
    {
        std::this_thread::sleep_for(std::chrono::seconds(3));
    }
    // 6.关闭信道
    conn->closeChannel(channel);
   


    return 0;
}